package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.business.rule.RuleBusinessLogger;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataDeleteBatchDTO;
import com.xbongbong.paas.pojo.dto.FormDataThoroughDeleteDTO;
import com.xbongbong.paas.pojo.vo.FormDataAddVO;
import com.xbongbong.paas.pojo.vo.FormDataDeleteBatchVO;
import com.xbongbong.paas.pojo.vo.FormDataThoroughDeleteVO;
import com.xbongbong.paas.pojo.workflow.task.vo.WorkFlowTaskLogListVO;
import com.xbongbong.paas.service.WorkflowOperatorDataService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.log.pojo.dto.RuleBusinessLogDTO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/09 15:55
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/workflowOperator")
public class WorkflowOperatorDataController {



    @Resource
    private WorkflowOperatorDataService workflowOperatorDataService;
    @Resource
    private RuleBusinessLogger ruleBusinessLogger;

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid FormDataAddDTO formDataAddDTO, BindingResult br) {
        XbbResponse<FormDataAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormDataAddVO formDataAddVO = workflowOperatorDataService.add(formDataAddDTO);
                response = new XbbResponse<>(formDataAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/writeLog", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String writeLog(@RequestBody @Valid RuleBusinessLogDTO ruleBusinessLogDTO, BindingResult br) {
        XbbResponse<WorkFlowTaskLogListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                List<UpdateDataEntity> valueList = ruleBusinessLogDTO.getValueList();
                ruleBusinessLogger.writeLog(ruleBusinessLogDTO, valueList);
                response = new XbbResponse<>();
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid FormDataDeleteBatchDTO formDataDeleteBatchDTO, BindingResult br) {
        XbbResponse<FormDataDeleteBatchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormDataDeleteBatchVO formDataDeleteBatchVO = workflowOperatorDataService.delete(formDataDeleteBatchDTO);
                response = new XbbResponse<>(formDataDeleteBatchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/thoroughDelete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String thoroughDelete(@RequestBody @Valid FormDataThoroughDeleteDTO formDataThoroughDeleteDTO, BindingResult br) {
        XbbResponse<FormDataThoroughDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormDataThoroughDeleteVO formDataDeleteBatchVO = workflowOperatorDataService.thoroughDelete(formDataThoroughDeleteDTO);
                response = new XbbResponse<>(formDataDeleteBatchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
